﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Code;
using System.Collections.Generic;
using System.Data.Common;

namespace E2_WEBSITE.Code.Comment
{
    public class CommentDataAccess
    {
        protected DataProvider _provider = DataProvider.GlobalDataProvider;

        public bool insertComment(int idUser, int idProduct, int iVote)
        {
            try
            {                               
                string sql = "INSERT INTO DANH_GIA(idNguoiDung, idSanPham, Diem) VALUES (" + idUser.ToString() + "," + idProduct.ToString() + "," + iVote.ToString()+ ");";
                return (_provider.ExecuteNonQuery(sql)>0);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool checkCommentExist(int idUser, int idProduct)
        {
            try
            {
                string sql = string.Format("SELECT * FROM DANH_GIA WHERE idNguoiDung = {0} AND idSanPham = {1}", idUser, idProduct);
                return (_provider.ExecuteQueryDataTable(sql).Rows.Count > 0);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool updateComment(int idUser, int idProduct, int iVote)
        {
            try
            {
                string sql = string.Format("UPDATE DANH_GIA SET Diem = {0} WHERE idSanPham = {1} AND idNguoiDung = {2}", iVote, idProduct, idUser);
                return (_provider.ExecuteNonQuery(sql) > 0);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<CommentInfo> getListCommentByIDProduct(int idProduct)
        {
            DataTable table = null;
            try
            {
                List<CommentInfo> lst = new List<CommentInfo>();

                string strSQL = "SELECT NGUOI_DUNG.Email, DANH_GIA.Diem FROM NGUOI_DUNG INNER JOIN DANH_GIA ON NGUOI_DUNG.id = DANH_GIA.idNguoiDung WHERE DANH_GIA.idSanPham = " + idProduct.ToString();
                
                table = _provider.ExecuteQueryDataTable(strSQL);
                foreach (DataRow row in table.Rows)
                {
                    CommentInfo tmp = new CommentInfo();
                    tmp.Member = new MemberInfo();
                    tmp.Member.Email = row["Email"].ToString();
                    tmp.Vote = int.Parse(row["Diem"].ToString());                                         
                    
                    lst.Add(tmp);
                }
                                        
                return lst;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (table != null)
                    table.Clear();
            }
        }
    }
}
